package art_of_java_concurrent_programming.volatile_future;

public class VolatileFeaturesExample {
    // 使用volatile声明64位的long型变量
    volatile long vl = 0L;

    public void set(long l) {
        // 单个volatile变量的写
        vl = l;
    }

    public void getAndIncrement() {
        // 复合（多个）volatile变量的读/写
        vl++;
    }

    public long get() {
        // 单个volatile变量的读
        return vl;
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileFeaturesExample example = new VolatileFeaturesExample();
        for (int i = 0; i < 1000; i++) {
            new Thread() {
                @Override
                public void run() {
                    example.getAndIncrement();
                }
            }.start();
        }
        Thread.sleep(5000);
        System.out.println(example.get());
    }
}